//引入express
const express = require('express');
// const { json } = require('express');
//创建应用对象
const app = express();
//创建路由规则
app.get('/server', (request, response) => {
    var data = {
        name:'光明',
        age:22,
        sex:'男',

    }
    var result = JSON.stringify(data)
    //设置响应头，使允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');
    //设置响应体
    response.send(result);
    // response.send('HELLO EXPRESS');

});
app.get('/ie', (request, response) => {
    //设置响应头，使允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');
    //设置响应体
    response.send('HELLO IE E');
    // response.send('HELLO EXPRESS');

});
app.get('/delay', (request, response) => {
    //设置响应头，使允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');
    //设置响应体
    setTimeout(() => {
        response.send('网络超时');
    }, 1000);
    // response.send('HELLO EXPRESS');

});
//jquery 请求响应
app.all('/jquery-server', (request, response) => {
    //设置响应头，使允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');
    //设置响应体
    var data = {
        name: "jquery",
        age: 290
    }
    var str = JSON.stringify(data);
    response.send(str);
    // response.send('HELLO JQUERY');
    // response.send('HELLO EXPRESS');

});
//axios请求Ajax
app.all('/axios-server', (request, response) => {
    //设置响应头，使允许跨域
    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader('Access-Control-Allow-Headers', '*');
    //设置响应体
    var data = {
        name: "jquery",
        age: 29
    }
    var str = JSON.stringify(data);
    response.send(str);
    // response.send('HELLO JQUERY');
    // response.send('HELLO EXPRESS');

});
//fetch请求Ajax
app.all('/fetch-server', (request, response) => {
    //设置响应头，使允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');
    response.setHeader('Access-Control-Allow-Headers', '*');
    //设置响应体
    var data = {
        name: "jquery",
        age: 290
    }
    var str = JSON.stringify(data);
    response.send(str);

});
app.post('/server', (request, response) => {
    //设置响应头，使允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');
    // response.setHeader('Access-Control-Allow-Header', '*');

    //设置响应体
    response.send('HELLO AJAX POST');
    // response.send('HELLO EXPRESS');

});
app.all('/json-server', (request, response) => {
    //设置响应头，使允许跨域
    response.setHeader('Access-Control-Allow-Origin', '*');
    // response.setHeader('Access-Control-Allow-Header', '*');
    const data = {
        name: 'json'
    };
    //设置响应体
    str = JSON.stringify(data);
    response.send(str);

    // response.send('HELLO AJAX json');
    // response.send('HELLO EXPRESS');

});
//监听端口启动服务
app.listen(8888, () => {
    console.log('服务已经启动，8000端口监听中。。。');

});